-- TarotM
-- created by panyl
-- 塔罗牌模块

module("TarotM", package.seeall);

local cards = {};

local rules = {};

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    cards = {};

    local tarot_card_CSV = get_tarot_card_CSV();
    for i = 1, #tarot_card_CSV do
        local row = tarot_card_CSV[i];
        cards[row.id] = row;
    end
end

-- 模块初始化操作
function init()
    if not _init then
        loadCsv();

        -- 载入所有的规则处理子模块
        rules = LOAD_PATH("game/logic/module/tarot");
    end
end

function query(cardId, path)
    local m = cards[cardId];

    if not m or not path then
        return m;
    end

    return m[path];
end

function getCardsByLevel(level)
    local ret = {};

    for id, row in pairs(cards) do
        if row["level"] <= level then
            table.insert(ret, id);
        end
    end

    table.sort(ret);

    return ret;
end

function fetchCard()
    local arr = {};

    -- 配置了权重的
    for id, row in pairs(cards) do
        if type(row["ratio"]) == 'number' then
            table.insert(arr, id);
        end
    end

    table.sort(arr);

    local rand = DungeonM.getRandSeed("tarot_fetch_card");
    local sum = 0;

    -- 根据权重抽取
    for index = 1, #arr do
        sum = sum + query(arr[index], "ratio");
    end
    rand = rand % sum;
    sum = 0;
    for index = 1, #arr do
        sum = sum + query(arr[index], "ratio");
        if (sum > rand) then
            return query(arr[index], "item_id");
        end
    end

    assert(0, "抽取塔罗牌失败。");
end

-- 翻牌
function drawCard(num)
    -- 如果不是在迷宫中
    if DungeonM.getState() ~= DUNGEON_GAMING then
        return;
    end

    local itemId;
    local bonus;

    for i = 1, num do
        itemId = fetchCard();
        if type(itemId) == 'number' then
            bonus = {1, itemId, 1};
            BonusM.doBonus(bonus, "tarot_bonus_item");
        end
    end

    -- 抛出事件
    --EventMgr.fire(event.DRAW_TAROT_CARD, {["cardId"] = cardId, });
end

function apply(cardId)
    local rule = query(cardId, "rule");
    local mod = rules[rule];

    if not mod or not mod.apply then
        return;
    end

    mod.apply(cardId);
end

function useCard(rule)
    -- 如果不是在迷宫中
    if DungeonM.getState() ~= DUNGEON_GAMING then
        return;
    end

    -- 获取对应效果ID
    local cardId = getIdByRule(rule);

    if cardId <= 0 then
        trace("TarotM", "找不到对应的塔罗牌效果ID");
        return;
    end

    local prop = PropM.combine(ME.user, "tarot", 1);
    if prop[3] > 0 then
        -- 触发特技发动事件
        EventMgr.fire(event.SPECIAL_SKILL_TRIGGER, { propId = prop[1] });
    end

    apply(cardId);
end

-- 根据规则获取对应等级的ID
function getIdByRule(rule)
    -- 塔罗牌技能等级
    local prop = PropM.combine(ME.user, "tarot", 1);
    local level = PropM.apply(prop, 0);

    if level < 1 then
        level = 1;
    end

    -- 根据需求，只会有一个ID
    for id, row in pairs(cards) do
        if row["rule"] == rule and row["level"] == level then
            return id;
        end
    end

    -- 没取到？
    return 0;
end
